<?php
session_start();
$action = $_GET["action"];
if($action == "loginout")
{
	unset($_SESSION["jdtoken"]);
}
$token = $_SESSION["jdtoken"];
$uid = $_SESSION["jduid"];
$usernick = $_SESSION["jdusernick"];

//保存操作
$tgurl = $_POST["tgurl"];
$tgname = $_POST["tgname"];
$tgdesc = $_POST["tgdesc"];
$tgparam = $_POST["tgparam"];
$action = $_POST["action"];
if($action == "save"){


	if($tgurl=="" || $tgname =="" || $tgparam=="")
	{
		echo "<script>alert('链接，名称，参数必须填写！')</script>";
	}
	else
	{
		$flag = true;
		if($token == "")
		{
			echo "<script>alert('请先登录')</script>";
			$flag = false;
		}
		if($uid != "您的京东用户ID")
		{
			echo "<script>alert('您没有权限保存，此功能只有我能操作哦，您可以下载源码部署到自己网站上运行')<script>";
			$flag = false;
		}
		
		if($flag){
			include("../lib/db.php");
			$conn = openDB();
			$maxsort = query("select max(sort) sort from linkUrl");
			$sortnumber = mysql_fetch_array($maxsort)["sort"] + 1;
			$sqlstring ="insert into linkUrl (urlString,urlName,urlType,urlParam,sort,`desc`)
			values('".$tgurl."','".$tgname."',1,'".$tgparam."',".$sortnumber.",'".$tgdesc."')";
			//echo $sqlstring;
			$result = query($sqlstring,$conn);
			 if(!$result){
				 echo "<script>alert('保存失败！')</script>";
			 }else{
				 echo "<script>alert('保存成功！')</script>";
			 }
		}
	}
}

?>

<!DOCTYPE html>
<html>
<head>
  <title>推广链接保存工具 From devonios.com</title>
  <meta charset="UTF-8">
  <script type="text/javascript" src="../static/js/jquery.js"></script>
  
	<style type="text/css">
		#mainContent{
		  margin-left: auto;
		  margin-right: auto;
		  margin-top:20px;
		  width:600px;
		  border:1px solid #CCC;
		}

		#urlgen,#urlform{

		  margin-left:30px;
		  margin-top:20px;
		  vertical-align:middle;
		}

		#loginjd,#mainContent,#urlform span{
		  color:#666;
		  font-size:14px;
		  font-family:"Microsoft YaHei";
		}

		#jdurl,#urlform input[type="text"]{
		  width:350px;
		  height:30px;
		}


		.savebtndiv{
		  text-align:center;
		  margin:20px auto 20px auto;
		}

		.btn{
		  background-color:#0066CC;
		  border:1px solid #ccc;
		  /*width:80px;*/
		  height:36px;
		  color:#fff;
		  cursor:hand;
		}
		#loginjd{
			text-align:center;
			margin-left: auto;
		  margin-right: auto;
		  margin-top:20px;
		  width:600px;
		}
	</style>
</head>

<body>
	<div id="loginjd">
		
		<?php
			if ($token == "")
			{
		?>
				<a href="token.php">登录</a>
		<?php
			}
			else
			{
		?>
				欢迎您<?php echo $usernick;?> <a href="?action=loginout">退出</a>
		<?php
			}
		?>
		
	</div>
	<div id="mainContent">
	  <div id="urlgen">
		  <span>京东URL-链接：</span>
		  <input type="text" id="jdurl" value=""/>
		  <div class="savebtndiv">
			<input type="button" id="btnGen" class="btn" value="生成JD联盟推广链接" onclick="gen()"/>
		  </div>
	  </div>
	  <div id="urlform">
		  <form id="form" method="post" onsubmit="return checkForm()" />
			  <input type="hidden" name="action" value="save"/>
			  <p>
				<span><a href="#" id="tgurlalink" target="_blank">推广URL-链接：</a></span>
				<input type="text" id="tgurl" name="tgurl" value=""/>
			  </p>
			  <p>
				<span>推广URL-名称：</span>
				<input type="text" id="tgname" name="tgname" value=""/>
			  </p>
			  <p>
				<span>推广URL-描述：</span>
				<input type="text" id="tgdesc" name="tgdesc" value=""/>
			  </p>
			  <p>
				<span>推广URL-参数：</span>
				<input type="text" id="tgparam" name="tgparam" value=""/>
			  </p>
			  <div class="savebtndiv">
				<input type="submit" class="btn" value="保存数据" />
			  </div>
		</form>
	  </div>
	</div>

  <script type="text/javascript">
    function gen(){
      var jdurl = $("#jdurl").val();
      if(jdurl){
        $.get("link.php", {url:jdurl,token:"<?php echo $token;?>"}, function (data,status){
            if(data){
              $("#tgurlalink").attr("href",data);
              $("#tgurl").val(data);
            }else{
              alert("生成url失败，请确认填写的链接是否正确");
            }
        });
      }else{
        alert("请输入京东链接");
        $("#jdurl").focus();
      }
    }
    function checkForm(){
      var tgurl = $("#tgurl").val();
      var tgname = $("#tgname").val();
      var tgdesc = $("#tgdesc").val();
      var tgparam = $("tgparam").val();

      if(tgurl =="" || tgname=="" || tgparam==""){
        alert("链接，名称，参数是必填项！");
        return false;
      }
      return true;
    }
  </script>
</body>
</html>
